package com.cadre.web.controller.officer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cadre.common.utils.R;
import com.cadre.common.utils.SecurityUtils;
import com.cadre.system.domain.officer.NavigatDept;
import com.cadre.system.service.NavigatDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping({"/profess/navigatDept"})
@RestController
/* loaded from: NavigatDeptController.class */
public class NavigatDeptController {
    @Autowired
    private NavigatDeptService navigatDeptService;

    @RequestMapping(value = {"/query/{userId}"}, method = {RequestMethod.GET})
    public R userdtamain(@PathVariable("userId") String userId) {
        return R.ok().put("roledata", (Object) this.navigatDeptService.getBaseMapper().selectList(new LambdaQueryWrapper<NavigatDept>().eq(NavigatDept::getUserid, userId)));
    }

    @RequestMapping(value = {"/selectunit/{userId}"}, method = {RequestMethod.GET})
    public R selectunit(@PathVariable("userId") String userId) {
        return R.ok().put("roledata", (Object) this.navigatDeptService.getBaseMapper().selectList(new LambdaQueryWrapper<NavigatDept>().eq(NavigatDept::getUserid, userId)));
    }

    /*  JADX ERROR: JadxRuntimeException in pass: BlockProcessor
        jadx.core.utils.exceptions.JadxRuntimeException: Unreachable block: B:22:0x0167
        	at jadx.core.dex.visitors.blocks.BlockProcessor.checkForUnreachableBlocks(BlockProcessor.java:86)
        	at jadx.core.dex.visitors.blocks.BlockProcessor.processBlocksTree(BlockProcessor.java:52)
        	at jadx.core.dex.visitors.blocks.BlockProcessor.visit(BlockProcessor.java:44)
        */
    @RequestMapping(value = {"/save"}, method = {RequestMethod.POST})
    @org.springframework.transaction.annotation.Transactional
    public R save(@RequestBody List<NavigatDept> list) {
        Long userId;
        if (list != null && list.size() != 0) {
            userId = ((NavigatDept)list.get(0)).getUserid();
            if (userId == null) {
                userId = SecurityUtils.getUserId();
            }

            this.navigatDeptService.remove((new LambdaQueryWrapper<NavigatDept>()).eq(NavigatDept::getUserid, userId));

            try {
                for(int i = 0; i < list.size(); ++i) {
                    String depitid = "0";
                    if (((NavigatDept)list.get(i)).getChild() != null) {
                        StringBuffer ids = new StringBuffer();
                        StringBuffer codes = new StringBuffer();

                        for(int j = 0; j < ((NavigatDept)list.get(i)).getChild().size(); ++j) {
                            ids.append(((NavigatDept)((NavigatDept)list.get(i)).getChild().get(j)).getDeptid() + ",");
                            codes.append(((NavigatDept)((NavigatDept)list.get(i)).getChild().get(j)).getUnitcode() + ",");
                        }

                        try {
                            ((NavigatDept)list.get(i)).setFlowid(UUID.randomUUID().toString().replace("-", ""));
                            ((NavigatDept)list.get(i)).setUserid(userId);
                            ((NavigatDept)list.get(i)).setDeptid(depitid);
                            String codeStr = codes.toString().substring(0, codes.length() - 1);
                            ((NavigatDept)list.get(i)).setUnitcode(codeStr);
                        } catch (Exception var8) {
                        }
                    }
                }
            } catch (Exception var9) {
                Exception ex = var9;
                System.out.println(ex.getMessage());
            }

            this.navigatDeptService.saveBatch(list);
            return R.ok();
        } else {
            userId = SecurityUtils.getUserId();
            this.navigatDeptService.remove((new LambdaQueryWrapper<NavigatDept>()).eq(NavigatDept::getUserid, userId));
            return R.ok();
        }
    }
}
